package servlet.man;

import bean.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import service.HygInfoService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 李夏
 * @version 1.0
 */
//管理员获取所有楼栋的卫生成绩总和
@WebServlet("/man/queryAllBuildHyg")
public class QueryAllBuildHygServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Integer>list =new ArrayList<>();
        for(int i=0;i<20;i++){
            try {
                list.add(HygInfoService.queryHygScore(i+1));
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        Result result = new Result();
        if(list!=null){
            result.success(list);
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }else{
            result.fail();
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }



    }
}
